package com.hawk.eye.server.delegate;

import com.hawk.eye.api.storage.ILogMessageStorage;
import com.hawk.eye.framework.exception.HawkEyeWebhookIdRepeatException;
import com.hawk.eye.framework.utils.ExceptionAssert;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @className: LogMessageStorageDelegate
 * @description:
 * @author: salad
 * @date: 2023/11/6
 **/
public class LogMessageStorageDelegate {

    private final ConcurrentHashMap<String, ILogMessageStorage> storageMap = new ConcurrentHashMap<>();

    private ILogMessageStorage defaultAlarmLogStorage;

    public void add(ILogMessageStorage logMessageStorage) {
        ExceptionAssert.isNotEmptyThrow(getAlarmLogStorage(logMessageStorage.identity()),
                () -> new HawkEyeWebhookIdRepeatException(ILogMessageStorage.class.getSimpleName()+ "[" + logMessageStorage.identity() + "]已经存在"));
        storageMap.put(logMessageStorage.identity(), logMessageStorage);
    }

    public ILogMessageStorage getAlarmLogStorage() {
        return defaultAlarmLogStorage;
    }

    public ILogMessageStorage getAlarmLogStorage(String storageType) {
        return storageMap.get(storageType);
    }

    public void setDefaultStorage(ILogMessageStorage defaultAlarmLogStorage) {
        this.defaultAlarmLogStorage = defaultAlarmLogStorage;
    }

}
